package view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import command.CloseTab;
import command.SendFile;
import command.SendMessage;
import controller.Contact;

@SuppressWarnings("serial")
public class DiscussionTab extends JPanel {

	protected Contact contact;
	
	protected JTextArea tsendMessage;
	protected ChatButton bsendMessage;
	protected JTextArea treceiveMessage;
	protected ChatButton bsendFile;
	protected ChatButton bcloseTab;
	
	protected JTabbedPane father;
	
	private boolean returnCloseDiscussion;
	private boolean returnDownloadACK;
	
	public DiscussionTab(Contact c, JTabbedPane father) {
		this.contact = c;
		this.father = father;
		initComponents();
	}

	public Contact getContact()
	{
		return contact;
	}
	
	private void initComponents()
	{		
		tsendMessage = new JTextArea();
		//tsendMessage.setPreferredSize( new Dimension(300, 80) );
		JScrollPane sendArea = new JScrollPane(tsendMessage);
		sendArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		sendArea.setPreferredSize( new Dimension(200, 50) );
		
		bsendMessage = new ChatButton("send", new SendMessage(tsendMessage, contact));
		bsendMessage.setSize(new Dimension(70, 40));
		
		bsendFile = new ChatButton("Send a file", new SendFile(this, contact));
		bsendFile.setSize(new Dimension(90, 40));
		
		bcloseTab = new ChatButton("close", new CloseTab(this));
		bcloseTab.setSize(new Dimension(90, 40));
		
		Box b1 = Box.createHorizontalBox();
		b1.add(sendArea);
		b1.add(bsendMessage);
		
		Box b2 = Box.createHorizontalBox();
		b2.add(bcloseTab);
		b2.add(bsendFile);
		
		Box b3 = Box.createVerticalBox();
		b3.add(b1);
		b3.add(b2);
		
		treceiveMessage = new JTextArea();
		treceiveMessage.setEditable(false);
		JScrollPane receiveArea = new JScrollPane(treceiveMessage);
		receiveArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		receiveArea.setPreferredSize( new Dimension(100, 250) );
		
		this.setLayout(new BorderLayout());
		this.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, receiveArea, b3), BorderLayout.CENTER);
	}
	
	private class DisplayMessage implements Runnable {
		String s;
		
		public DisplayMessage(String s)
		{
			this.s = s;
		}
		
		public void run()
		{
			treceiveMessage.append(s);
		}
	}
	
	public void displayMessage(String s)
	{
		SwingUtilities.invokeLater(new DisplayMessage(s));
	}
	
	private class DisplayDemand implements Runnable {
		private String message;
		private JPanel win;
		
		public DisplayDemand(String message, JPanel win)
		{
			this.message = message;
			this.win = win;
		}
		
		public void run()
		{
			int option = JOptionPane.showConfirmDialog(win, message, "Download request", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

		    if(option == JOptionPane.NO_OPTION || option == JOptionPane.CLOSED_OPTION)
		    	returnDownloadACK = false;
		    else
		    	returnDownloadACK = true;
		}
	}
	
	public boolean displayDemand(String message)
	{	
		try
		{
			SwingUtilities.invokeAndWait(new DisplayDemand(message, this));
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		return returnDownloadACK;
	}
	
	private class DisplayTransferNotif implements Runnable {
		private String message;
		private JPanel win;
		
		public DisplayTransferNotif(String message, JPanel win)
		{
			this.message = message;
			this.win = win;
		}
		
		public void run() {
			JOptionPane.showMessageDialog(win, message, "Upload information", JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	public void displayTranferInformation(String message)
	{
		try
		{
			SwingUtilities.invokeLater(new DisplayTransferNotif(message, this));
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private class DisplayCloseWindow implements Runnable {
		private String s;
		private JPanel win;
		
		public DisplayCloseWindow(String s, JPanel win)
		{
			this.s = s;
			this.win = win;
		}
		
		public void run()
		{
			int option = JOptionPane.showConfirmDialog(win, 
			        s + "\nVoulez-vous quitter la discussion ?",
			        "Fermeture de la discussion", 
			        JOptionPane.YES_NO_OPTION, 
			        JOptionPane.QUESTION_MESSAGE);
			
			if(option == JOptionPane.NO_OPTION)
				returnCloseDiscussion = false;
			else
				returnCloseDiscussion = true;
		}
	}
	
	public boolean displayCloseWindow(String s)
	{
		try
		{
			SwingUtilities.invokeAndWait(new DisplayCloseWindow(s, this));
		}
		catch(Exception e) {}
		
		return returnCloseDiscussion;
	}
}
